package com.digitalchina.lxnsy.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.base.ListBaseAdapter;
import com.digitalchina.lxnsy.entity.DataMallVO;
import com.digitalchina.lxnsy.toolkit.ToastUtil;


/**
 * 参考：https://github.com/mcxtzhang/SwipeDelMenuViewGroup
 */

public class SwipeMenuAdapter extends ListBaseAdapter<DataMallVO> {
    private Context mContext;
    private LayoutInflater mInfalter;

    public SwipeMenuAdapter(Context context) {
        mContext = context;
        mInfalter = LayoutInflater.from(context);
    }

    @Override
    public SwipeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SwipeViewHolder(mInfalter.inflate(R.layout.list_item_swipe, parent, false));
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        SwipeViewHolder viewHolder = (SwipeViewHolder) holder;
        //这句话关掉IOS阻塞式交互效果 并依次打开左滑右滑
//        ((SwipeMenuView) viewHolder.itemView).setIos(false).setLeftSwipe(position % 2 == 0 ? true : false);

        viewHolder.tvTitle.setText(getDataList().get(position).getTitle());
        viewHolder.tvSummary.setText(getDataList().get(position).getSummary());
        viewHolder.tvPrice.setText(String.format("￥ %s", getDataList().get(position).getAmount_num()));
        viewHolder.tvType.setText(getDataList().get(position).getResourceType());

        //隐藏控件
        viewHolder.btnUnRead.setVisibility(position % 3 == 0 ? View.GONE : View.VISIBLE);

        viewHolder.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mOnSwipeListener) {
                    //如果删除时，不使用mAdapter.notifyItemRemoved(pos)，则删除没有动画效果，
                    //且如果想让侧滑菜单同时关闭，需要同时调用 ((CstSwipeDelMenu) holder.itemView).quickClose();
                    //((CstSwipeDelMenu) holder.itemView).quickClose();
                    mOnSwipeListener.onDel(position);
                }
            }
        });
        //注意事项，设置item点击，不能对整个holder.itemView设置咯，只能对第一个子View，即原来的content设置，这算是局限性吧。
        (viewHolder.contentView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.showToast(mContext, getDataList().get(position).getTitle());
                Log.d("TAG", "onClick() called with: v = [" + v + "]");
            }
        });
        //置顶：
        viewHolder.btnTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mOnSwipeListener) {
                    mOnSwipeListener.onTop(position);
                }

            }
        });
    }

    /**
     * 和Activity通信的接口
     */
    public interface onSwipeListener {
        void onDel(int pos);

        void onTop(int pos);
    }

    private onSwipeListener mOnSwipeListener;

    public void setOnDelListener(onSwipeListener mOnDelListener) {
        this.mOnSwipeListener = mOnDelListener;
    }

    private class SwipeViewHolder extends RecyclerView.ViewHolder {
        View contentView;
        private ImageView ivImage;
        private TextView tvTitle;
        private TextView tvSummary;
        private TextView tvPrice;
        private TextView tvType;
        Button btnDelete;
        Button btnUnRead;
        Button btnTop;

        SwipeViewHolder(View itemView) {
            super(itemView);
            contentView = itemView.findViewById(R.id.swipe_content);
            tvTitle = (TextView) itemView.findViewById(R.id.item_tv_title);
            tvSummary = (TextView) itemView.findViewById(R.id.item_tv_summary);
            tvPrice = (TextView) itemView.findViewById(R.id.item_tv_price);
            tvType = (TextView) itemView.findViewById(R.id.item_tv_type);
            btnDelete = (Button) itemView.findViewById(R.id.btnDelete);
            btnUnRead = (Button) itemView.findViewById(R.id.btnUnRead);
            btnTop = (Button) itemView.findViewById(R.id.btnTop);
        }
    }
}

